package user

import (
	"log"
	"web_simple/util"
)

/**数据库实体Model**/
type User struct {
	ID       uint   `gorm:"primaryKey;column:id" json:"id"`
	UserNo   int    `gorm:"column:user_no" json:"userNo" binding:"required"`
	UserName string `gorm:"column:user_name" json:"userName" binding:"required"`
	Age      int    `gorm:"column:age" json:"age"`
}

// 自定义表名: 表名 user -- 实体名 User
func (User) TableName() string {
	return "user"
}

func CreateUser(user User) bool {
	db := util.MysqlConnection()
	res := db.Create(&user)
	if res.Error != nil {
		log.Fatal("create User failed")
		return false
	}
	return true
}

func UpdateUser(user User) bool {

	return true
}

func DeleteUser(userId int) bool {

	return true
}

func getUserById(userId int) User {
	db := util.MysqlConnection()
	var user User
	db.First(&user, userId)
	return user
}

func getUserList() []User {
	db := util.MysqlConnection()
	var users []User
	db.Find(&users)
	return users
}
